<?

/* GmController controller class.
 * This class contains the handling code, divided into action methods.
 *
 */ 
/* @author Udo Schroeter <udo.schroeter@gmail.com>
 * @license dual license: LGPL / commercial use license
 */ 

class GmController extends AGController
{
  function __construct($ctrName) 
  {
    parent::__construct($ctrName);
    include_once('lib/cq-forms.php');
    $this->refFields['s_item'] = array('table' => 'items', 'caption' => 'i_name');
    $this->refFields['s_zone'] = array('table' => 'zones', 'caption' => 'z_name');
    $this->refFields['a_zone'] = array('table' => 'zones', 'caption' => 'z_name');
    $this->refFields['c_race'] = array('table' => 'races', 'caption' => 'r_name');
    $this->refFields['c_location'] = array('table' => 'zones', 'caption' => 'z_name');
  }
  
  function __init()
  {
    if ($this->user->localRecord['u_type'] != 'gm')
      die();
  }
  
  function Notifications()
  {
    
  }
  
  function FeedTemplates()
  {
  
  }

  function Index()
  {
    // your default action code goes here
    
  }
  
  function Zone()
  {
    $this->_handleInputForm('zones', 'z_name');
  }
  
  function News()
  {
    $this->_handleInputForm('news', 'n_title');
  }
  
  function News_Comment()
  {
    $this->_handleInputForm('news_comments', 'nc_date');
  }
  
  function Action()
  {
    $this->_handleInputForm('actions', 'z_name,a_name', 'zones', 'a_zone');
  }
  
  function Spell()
  {
    $this->_handleInputForm('spells', 'sp_level,sp_name');
  }
  
  function User()
  {
    $this->_handleInputForm('users', 'u_fb_name');
  }
  
  function Character()
  {
    $this->_handleInputForm('characters', 'u_type,c_name', 'users', 'c_ref_user');
  }
  
  function Shop_Item()
  {
    $this->_handleInputForm('shopitems', 'z_name,i_name', 'zones,items', 's_zone,s_item');
  }
  
  function Item()
  {
    $this->_handleInputForm('items', 'i_name');
  }
  
  function Race()
  {
    $this->_handleInputForm('races', 'r_name');
  }
  
  function _HandleInputForm($table, $namefield, $jointable = '', $joinfield = '')
  {
    $keys = DB_GetKeys($table);
    $keyfield = $keys[0];
    if ($jointable != '')
    {
      $jfield = explode(',', $joinfield);
      $fldctr = -1;
      foreach(explode(',', $jointable) as $jtable)
      {
        $fldctr++;
        $jkeys = DB_GetKeys($jtable);
        $jkeyfield = $jkeys[0];
        $this->join .= ' LEFT JOIN '.getTableName($jtable).' ON ('.$jfield[$fldctr].'='.$jkeyfield.') ';
      }
    }
    $this->keyfield = $keyfield;
    $this->namefield = $namefield;
    $this->tablename = $table;
    $_REQUEST['z'] = getDefault($_REQUEST[$keyfield], $_REQUEST['z']);
    if ($_REQUEST['z'] == 'new')
    {
      $nds = array();
      $_REQUEST['z'] = DB_UpdateDataset($table, $nds);
    }
    $this->currentZone = getDefault($_REQUEST['z'], 1);
    $this->zds = DB_GetDataset($table, $this->currentZone);
    $this->form = new CQForm($table);
    $this->form->ds = &$this->zds;
    $this->form->addFromDataset($this->zds, $table);
    foreach($this->form->elements as $k => $el)
    {
      $ref = $this->refFields[$k];
      if (is_array($ref)) 
      {
        $this->form->elements[$k]['type'] = 'dropdown';
        $keys = DB_GetKeys($ref['table']);
        foreach(DB_GetList('SELECT * FROM '.getTableName($ref['table']).' ORDER BY '.$ref['caption']) as $op)
          $this->form->elements[$k]['options'][$op[$keys[0]]] = $op[$ref['caption']];
      }
    }
    if ($this->form->submitted && $this->form->getData())
    {
      DB_UpdateDataset($table, $this->form->ds);
    }
    $this->viewName = 'zone';
  }
}


?>
